<?php

namespace app\admin\controller\api;

use app\common\model;

/**
 * 楼栋
 * @internal
 */
class Building extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 楼栋信息
     *
     * @User ZhenZiHan
     * @Date 2019/12/24
     *
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $id = $this->request->request('id');

        $result = model\Building::get($id);

        $this->success('', $result);
    }

    /**
     * 楼栋信息 「包含园区、学校信息」
     *
     * @User ZhenZiHan
     * @Date 2019/12/24
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function profile()
    {
        $id = $this->request->request('id');
        $result =  model\Building::alias('building')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->join('__SCHOOL__ school', 'school.id = garden.school_id', 'left')
            ->where('building.id', $id)
            ->field('building.*,garden.number garden_number,garden.name garden_name,school.id school_id,school.name school_name')
            ->find();

        $this->success('', $result);
    }

    /**
     * 楼栋列表
     *
     * @User ZhenZiHan
     * @Date 2019/12/24
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $where = $this->request->request('where');
        $sort = $this->request->request('sort');
        $order = $this->request->request('order');
        $offset = $this->request->request('offset');
        $limit = $this->request->request('limit');
        $type = $this->request->request('type', 0);

        $school_ids = $this->getSchoolIds($this->_user['id']);

        $total = model\Building::alias('building')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->join('__SCHOOL__ school', 'school.id = garden.school_id', 'left')
            ->where($where)
            ->where(function ($query) use ($type, $school_ids) {
                if ($type) {
                    $query->whereIn('building.type', [$type, model\Building::MIX]);
                }
                if (!in_array('*', $school_ids)) {
                    $query->whereIn('building.school_id', $school_ids);
                }
            })
            ->order($sort, $order)
            ->count();

        $list = model\Building::alias('building')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id', 'left')
            ->join('__SCHOOL__ school', 'school.id = garden.school_id', 'left')
            ->where($where)
            ->where(function ($query) use ($type, $school_ids) {
                if ($type) {
                    $query->whereIn('building.type', [$type, model\Building::MIX]);
                }
                if (!in_array('*', $school_ids)) {
                    $query->whereIn('building.school_id', $school_ids);
                }
            })
            ->field('building.*,garden.number garden_number,garden.name garden_name,school.id school_id,school.name school_name')
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        if ($list) {
            $list = collection($list)->append(['type_text'])->toArray();
        }

        $result = array("total" => $total, "rows" => $list);

        $this->success('', $result);

    }

    public function detail()
    {
        $school_id = $this->request->request('school_id');
        $garden_number = $this->request->request('garden_number');
        $building_number = $this->request->request('building_number');

        $result = model\School::alias('school')
            ->join('__GARDEN__ garden', 'garden.school_id = school.id')
            ->join('__BUILDING__ building', 'building.garden_id = garden.id')
            ->where('school.id', $school_id)
            ->where('garden.number', $garden_number)
            ->where('building.number', $building_number)
            ->field('school.id school_id,school.name school_name,garden.id garden_id,garden.number garden_number,garden.name garden_name,building.id building_id,building.number building_number,building.name building_name')
            ->find();

        $this->success('', $result);
    }

}